
"""
test sequence usage in python
created by cheka, 2010-7-4, a lonely sunday
"""

import unittest

class TestSequence(unittest.TestCase):
    
    def test_in(self):
        asequence = range(1,10)
        self.assertTrue(1 in asequence) # start point included in the sequence
        self.assertTrue(2 in asequence)
        
        self.assertFalse(10 in asequence) # end point will not be included
        self.assertTrue(10 not in asequence)
        
        # ------------------ can not use "in" to check whether a sub-range exists
        self.assertFalse([1,2] in asequence)
        self.assertFalse((1,2) in asequence)
        
        # ------------------ "in" can only used to check an element
        # "in" use "==" to determine equlity
        asequence[0] = [1,2]
        self.assertTrue([1,2] in asequence)
        
    def test_index(self):
        names = ("cheka","mss","stasi","cia")
        
        self.assertEqual("cia",names[-1])
        self.assertEqual("cheka",names[-(len(names))])
        
        self.assertRaises(IndexError,lambda : names[len(names) ])
        
    def test_slice(self):
        asequence = range(1,4)
        
        self.assertEqual([1,2,3],asequence)
        self.assertEqual([1,2,3],asequence[:])
        # starting and ending position can exceed the length of the sequence
        self.assertEqual([1,2,3],asequence[:100])
        
        self.assertEqual([2,3],asequence[1:])
        self.assertEqual([1],asequence[:1])
        
        # ======================== test negative index
        self.assertEqual([1,2],asequence[:-1])
        self.assertEqual([1],asequence[:-2])
        
    def test_slice_stride(self):
        asequence = range(1,10)
        self.assertEqual([9,8,7,6,5,4,3,2,1],asequence[::-1])
        self.assertEqual([1,3,5,7,9],asequence[::2])
        
    def test_range(self):
        # ascending order
        self.assertEqual([1,2,3],range(1,4))
        # empty
        self.assertEqual([],range(4,1))
        # descending order
        self.assertEqual([4,3,2],range(4,1,-1))
        
if __name__ == "__main__":
    unittest.main()